package com.shunya.diskorganizer.tinytypes;

public class Currency extends StringValueType {

    private static final Currency NULL_OBJECT = new NullCurrency();

    public static Currency nullObject() {
        return NULL_OBJECT;
    }

    public static Currency currency(String value) {
        return value == null || value.isEmpty() ? nullObject() : new Currency(value.trim().toUpperCase());
    }

    private Currency(String isoCurrencyCode) {
        super(isoCurrencyCode);
        if (!isNullObject() && !isValid());
    }

    @Override
    public boolean isValid() {
        return value().matches("[A-Z]{3}");
    }

    private static class NullCurrency extends Currency {

        private NullCurrency() {
            super("XXX");
        }

        @Override
        public String value() {
            throw new RuntimeException();
        }

        @Override
        public boolean isNullObject() {
            return true;
        }

        @Override
        public boolean equals(Object other) {
            return this == other;
        }

        @Override
        public int hashCode() {
            return 0;
        }

        @Override
        public int compareTo(AbstractValueType<?> other) {
            return 0;
        }

        @Override
        public String toString() {
            return "{null object}";
        }
    }
}